package com.hurl.waguji.biz.download;

import org.apache.log4j.Logger;

import com.hurl.library.spider.Spider;
import com.hurl.library.spider.SpiderConfig;
import com.hurl.library.spider.SpiderException;
import com.hurl.waguji.base.dataprocess.ProcessResult;
import com.hurl.waguji.base.dataprocess.ProcessException;
import com.hurl.waguji.base.dataprocess.ProcessIdentifier;
import com.hurl.waguji.base.dataprocess.ProcessParams;

/**
 * 代表一个下载项的唯一标识。
 * @author hurl
 *
 */
public abstract class DownloadIdentifier extends ProcessIdentifier{

	public static final DownloadIdentifier CW_CWBL_HEXUN = new CwCwblHexunIdentifier();
	public static final DownloadIdentifier CW_CWGC_THS = new CwCwgcThsIdentifier();
	public static final DownloadIdentifier CW_CWZB_THS = new CwCwzbThsIdentifier();
	public static final DownloadIdentifier CW_LRB_HEXUN = new CwLrbHexunIdentifier();
	public static final DownloadIdentifier CW_LRB_THS = new CwLrbThsIdentifier();
	public static final DownloadIdentifier CW_XJLB_THS = new CwXjlbThsIdentifier();
	public static final DownloadIdentifier CW_ZCFZB_HEXUN = new CwZcfzbHexunIdentifier();
	public static final DownloadIdentifier CW_ZCFZB_THS = new CwZcfzbThsIdentifier();
	public static final DownloadIdentifier HYTJ_THS = new HytjThsIdentifier();
	public static final DownloadIdentifier MRHQ_CJMX_FBMX_HEXUN = new MrhqCjmxFbmxHexunIdentifier();
	public static final DownloadIdentifier MRHQ_CJMX_FJMX_HEXUN = new MrhqCjmxFjmxHexunIdentifier();
	public static final DownloadIdentifier MRHQ_CJMX_HEXUN = new MrhqCjmxHexunIdentifier();
	public static final DownloadIdentifier MRHQ_HEXUN = new MrhqHexunIdentifier();
	public static final DownloadIdentifier MRHQ_SINA = new MrhqSinaIdentifier();
	public static final DownloadIdentifier PROFILE_GBJG_THS = new ProfileGbjgThsIdentifier();
	public static final DownloadIdentifier PROFILE_GDRS_THS = new ProfileGdrsThsIdentifier();
	public static final DownloadIdentifier PROFILE_GD_THS = new ProfileGdThsIdentifier();
	public static final DownloadIdentifier PROFILE_GSGG_THS = new ProfileGsggThsIdentifier();
	public static final DownloadIdentifier PROFILE_GSGK_THS = new ProfileGsgkThsIdentifier();
	public static final DownloadIdentifier PROFILE_JJSJB_THS = new ProfileJjsjbThsIdentifier();
	public static final DownloadIdentifier PROFILE_FH_HEXUN = new ProfileFhHexunIdentifier();
	public static final DownloadIdentifier PROFILE_PG_HEXUN = new ProfilePgHexunIdentifier();
	public static final DownloadIdentifier STOCK_CODE_HEXUN = new StockCodeHexunIdentifier();
	public static final DownloadIdentifier MRHQ_CJMX_FILE_TENCENT = new MrhqCjmxFileTencentIdentifier();
	public static final DownloadIdentifier MRHQ_CJMX_TENCENT = new MrhqCjmxTencentIdentifier();

	private static final Logger log = Logger.getLogger(DownloadIdentifier.class);
	
	protected abstract BaseDownloadProcessor newProcessor(ProcessParams params);

	public DownloadIdentifier(String id, String title) {
		super(id, title);
	}
	public synchronized ProcessResult execute(ProcessParams params)throws ProcessException{
		BaseDownloadProcessor processor = this.newProcessor(params);
		Spider spider = new Spider();
		
		SpiderConfig config = new SpiderConfig();
		config.setUrlPattern(processor.getUrlPattern());
		spider.setConfig(config);
		
		spider.addListener(processor);

		try {
			spider.start();
		} catch (SpiderException e) {
			e.printStackTrace();
			throw new ProcessException("下载出错："+e.getMessage());
		}
		
		ProcessResult result = processor.getDownloadResult();
		log.debug(result);
		return result;
	}
}
